package logic;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

//		This class sends mail from our bcbs mail to the user, via. gmail, who transfer/deposits BTC
public class ReceiptMail {

	String receiptSent;

	public ReceiptMail(String email) {

		final String username = "bcbssystems@gmail.com";
		final String password = "bcbs12345";

		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "587");

		Session s = Session.getInstance(props, new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		});

		try {

			Message m = new MimeMessage(s);

			m.setFrom(new InternetAddress("bcbssystems@gmail.com"));
			// Her skal vi have hentet mailen fra databasen og sat ind i til:
			// "mail"
			m.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(email));
			m.setSubject("Your BCBS Receipt - No reply");
			m.setText("Your Receipt\n\n Nice doin' business with ya!");

			MimeBodyPart messageBodyPart = new MimeBodyPart();

			Multipart multipart = new MimeMultipart();

			messageBodyPart = new MimeBodyPart();
			String file = "C:/Users/Niels/Documents/Eclipse/BitcoinCBS/Receipt"
					+ email + ".pdf";
			String fileName = "Receipt" + email + ".pdf";
			DataSource source = new FileDataSource(file);
			messageBodyPart.setDataHandler(new DataHandler(source));
			messageBodyPart.setFileName(fileName);
			multipart.addBodyPart(messageBodyPart);

			m.setContent(multipart);

			System.out.println("Sending");

			Transport.send(m);

			System.out.println("Done");

			receiptSent = new String("sent");

		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
	}

	public String getSent() {
		return receiptSent;
	}

}